<?php


namespace app\admin\controller;

use app\common\model\MiniApp;
use app\common\model\Movie as MovieModel;
use app\common\model\WechatShareLink;
use think\Controller;
use think\Db;
use think\facade\Request;

class Movie extends Controller
{

    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'
    ];

    //指定用户的电影列表
    public function index()
    {
        //状态码
        $status = Request::param('status', 0);
        $page = Request::param('page', 1);
        $pageSize = Request::param('pageSize', 20);           //哪一天的数据

        if($status==0){
            $where=[
                'mini_app_code'=> 'movie',
                'video_url'=> 0,
            ];
        }else{
            $where=[
                ['mini_app_code','=', 'movie'],
                ['video_url','<>', 0],
            ];
        }

        $list = WechatShareLink::where($where)->page($page, $pageSize)
            ->withAttr('add_time', function ($value, $data) {
                return date("Y-m-d H:i:s", $value);
            })
            ->order('id','desc')
            ->select();

        $count = WechatShareLink::where($where)
            ->count();

        $data = [
            'list' => $list,
            'page_num' => ceil($count / $pageSize),
            'page' => $page
        ];

        $this->result($data, 200, '成功');
    }

    //下线视频
    public function offlineVideo()
    {
        $url_id= Request::param('url_id', 0);

        $res = WechatShareLink::where('id', $url_id)
            ->update([
                "video_url" => 0,
                "user_id" => 0,
            ]);

        $this->result('', 200, '成功');
    }

    //更改状态
    public function changeMovieStatus()
    {
        $status = Request::param('status', 0);
        $url_id= Request::param('url_id', 0);

        $res = WechatShareLink::where('id', $url_id)
            ->update([
                "status" => $status
            ]);

        if ($res) {
            $this->result('', 200, '成功');
        } else {
            $this->result('', 400, '失败');
        }
    }

    //设置
    public function setting()
    {
        $data = Request::only(['name', 'desc', 'logo','classify', 'vip', 'android', 'ios', 'user_rate','vip_rate', 'lv_1_rate', 'lv_2_rate', 'wx_appid', 'wx_secret', 'tt_appid', 'tt_secret', 'wx', 'tt', 'status', 'views']);
        $data['code'] = 'movie';
        //设置查剧名的资料
        MiniApp::setAppData($data['code'], $data);
        $this->result($data, 200, '成功');
    }


    public function settingRead()
    {
        $data=MiniApp::where('code','movie')->find();

        $this->result($data, 200, '成功');
    }

}
